package joyson.email.service.impl;

import joyson.email.properties.EmailProperties;
import joyson.email.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;

/**
 * @description:
 * @Author ke
 * @Date 2024/5/18 14:48
 */
@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    private EmailProperties emailProperties;

    @Override
    public void handleBirthDay(Map<String, String> params) {

        final String BIRTH_DAY_OLD = emailProperties.getPath() + "/old/birth.jpg";
        final String BIRTH_DAY_NEW =  emailProperties.getPath() + "/new/birth.jpg";
        try {
            File oldFile = new File(BIRTH_DAY_OLD);
            File newFile = new File(BIRTH_DAY_NEW);

            BufferedImage srcImg = ImageIO.read(oldFile);

            int startHeight = 0;  //表格的起始高度
            int startWidth = 0;   //表格的起始宽度

            Graphics2D g = srcImg.createGraphics();
            g.setColor(Color.BLACK);
            Font nameFont = new Font("黑体", Font.BOLD, 45);
            g.setFont(nameFont);
            //名字
            String name = params.get("name") + ":";
            g.drawString(name, startWidth + 345, startHeight + 890);
            Font helloFont = new Font("arial", Font.BOLD, 45);
            g.setFont(helloFont);
            //hello
            g.drawString("hello! ", startWidth + 215, startHeight + 890);
            g.dispose();
            ImageIO.write(srcImg, "JPG", newFile);
            params.put("picture", BIRTH_DAY_NEW);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void handleAnniversary(Map<String, String> params) {
        final String ANNIVERSARY_OLD =  emailProperties.getPath() + "/old/anniversary.jpg";
        final String ANNIVERSARY_NEW =  emailProperties.getPath() + "/new/anniversary.jpg";
        try {

            File oldFile = new File(ANNIVERSARY_OLD);
            File newFile = new File(ANNIVERSARY_NEW);

            BufferedImage srcImg = ImageIO.read(oldFile);

            int startHeight = 0;  //表格的起始高度
            int startWidth = 0;   //表格的起始宽度

            Graphics2D g = srcImg.createGraphics();
            g.setColor(Color.BLACK);
            Font nameFont = new Font("黑体", Font.BOLD, 45);
            g.setFont(nameFont);
            g.drawString("柯鸿霖:", startWidth + 345, startHeight + 890);
            Font helloFont = new Font("arial", Font.BOLD, 45);
            g.setFont(helloFont);
            g.drawString("hello!", startWidth + 220, startHeight + 890);
            g.dispose();
            ImageIO.write(srcImg, "JPG", newFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
